// src/utils/request.js

import axios from 'axios'
import { ElMessage } from 'element-plus'

const instance = axios.create({
  baseURL: '/',
  timeout: 50000,
})

// 添加请求拦截器
instance.interceptors.request.use(config => {
  if (!config.headers['Content-Type'] && !(config.data instanceof FormData)) {
    config.headers['Content-Type'] = 'application/json'
  }
  return config
}, error => {
  return Promise.reject(error)
})

instance.interceptors.response.use(
  function (response) {
    if (response.config.responseType === 'arraybuffer') {
        return response; // 对于二进制文件，直接返回整个响应体
    }
    
    if (response.status === 200) {
      return response.data
    }
    ElMessage.error('服务异常')
    return Promise.reject(response.data)
  },
  function (error) {
    ElMessage.error('服务异常')
    return Promise.reject(error)
  }
)

export default instance